#include "StdAfx.h"
#include "Camera.h"

VAMPIRE_NAMESPACE_BEGIN

Camera::Camera(const TString& name)
: Frustum(name), mPosition(0.0f, 0.0f, 0.0f), mLookAt(0.0f, 0.0f, 1.0f), mUp(0.0f, 1.0f, 0.0f)
{
	viewerTransform();
}

Camera::Camera( const TString& name, const Vector3& position, const Vector3& lookAt, const Vector3& up )
: Frustum(name), mPosition(position), mLookAt(lookAt), mUp(up)
{
	viewerTransform();
}

const Vector3& Camera::getPosition() const
{
	return mPosition;
}

void Camera::setPosition( const Vector3& position )
{
	mPosition = position;
	viewerTransform();
}

const Vector3& Camera::getLookAt() const
{
	return mLookAt;
}

void Camera::setLookAt( const Vector3& lookAt )
{
	mLookAt = lookAt;
	viewerTransform();
}

const Vector3& Camera::getUp() const
{
	return mUp;
}

void Camera::setUp( const Vector3& up )
{
	mUp = up;
	viewerTransform();
}

void Camera::viewerTransform()
{
	if (mAttachedNode)
	{
		mPosition = mAttachedNode->getWorldSpacePosition();
		mLookAt = mPosition + mAttachedNode->getWorldSpaceOrientation() * Vector3::UNIT_Z;
		mUp = mAttachedNode->getWorldSpaceOrientation() * Vector3::UNIT_Y;
	}
	D3DXMatrixLookAtLH(&(mViewMatrix.mData), &(mPosition.mData), &(mLookAt.mData), &(mUp.mData));
	gpDevice->SetTransform(D3DTS_VIEW, &(mViewMatrix.mData));
}

Matrix44 Camera::getViewProjMatrix() const
{
	return mViewMatrix * mProjectionMatrix;
}

Matrix44 Camera::getViewMatrix() const
{
	return mViewMatrix;
}


VAMPIRE_NAMESPACE_END